package com.ruoyi.web.controller.llm;

import com.alibaba.fastjson2.JSONObject;
import com.github.pagehelper.PageHelper;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.llm.domain.ChatHistory;
import com.ruoyi.llm.domain.po.ChatHistoryPo;
import com.ruoyi.llm.domain.vo.ChatHistoryVo;
import com.ruoyi.llm.domain.vo.ChatMqDataVo;
import com.ruoyi.llm.domain.vo.SendChatVo;
import com.ruoyi.llm.service.ChatHistoryService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.List;

@Api(tags="聊天接口")
@RestController
@RequestMapping("/llm/chat")
public class ChatHistoryController extends BaseController {

    @Autowired
    private ChatHistoryService chatHistoryService;


    @GetMapping("/chatlist")
    @ApiOperation(value = "查看每个会话详细记录",response= ChatHistoryPo.class)
    public AjaxResult chatList(@RequestParam("sessionId") String sessionId, @RequestParam("projectId") String projectId){
        List<ChatHistoryPo> ls = chatHistoryService.getChatDetail(sessionId,projectId);
        return this.success(ls);
    }

    @PostMapping("/history")
    @ApiOperation(value = "历史列表",response= ChatHistory.class)
    public AjaxResult projectList(@RequestBody ChatHistory chatHistory)
    {
        chatHistory.setEndTime(DateUtils.addDays(chatHistory.getEndTime(),1));
        List<ChatHistoryVo> ls=chatHistoryService.getChatHistoryList(chatHistory);
        return this.success(ls);
    }


    @PostMapping("/chatFirst")
    @ApiOperation(value = "第一次对话")
    public AjaxResult getChatFirst(@RequestBody SendChatVo sendChat)
    {
        return this.success(chatHistoryService.getWelcome(sendChat));
    }

    @PostMapping("/answer")
    @ApiOperation(value = "回复结果")
    public AjaxResult getChat(@RequestBody SendChatVo sendChat)
    {
        ChatMqDataVo chatMqData =  chatHistoryService.getChatJsonMap(sendChat);
        JSONObject sendJson = new JSONObject();
        sendJson.put("taskData",chatMqData.getTaskData());
        sendJson.put("taskId",chatMqData.getTaskId());
        sendChat.setAnswer(chatHistoryService.sentMQ(chatMqData.getTaskId(),chatMqData.getNoansertxt(),sendJson));
        return this.success(sendChat);
    }
}
